<?php

namespace App\Http\Services;

use App\Models\ComboSpec;
use App\Models\ComboSpecItem;
use App\Models\MemberLabel;
use App\Models\MenuProductMapping;
use App\Models\OrderProduct;
use App\Models\OrderSku;
use App\Models\Product;
use Cache;
use Illuminate\Support\Facades\DB;


/**
 * 匹配池
 */
class HggsyService
{
    private $_connection = null;

    public function __construct()
    {
        $this->_connection = DB::connection("db2");
    }

    public function batchData()
    {
//        $this->updateMemberTags();   去掉

        if (Cache::get('batch')) {
            // return false;
        }

        Cache::set("batch", true);
        // $this->updateProductCombo();


        // $this->dailyCombo();

        $this->orderSku();

        Cache::set("batch", false);
    }

    /**
     * 更新会员标签
     */
    public function updateMemberTags()
    {
        $memberList = $this->_connection->table("member")->where('type', '>', 0)
            ->where("id", '>', 0)->get(['id', 'type']);

        $data = [];
        foreach ($memberList as $k => $v) {
            $data[] = [
                'uid' => $v->id,
                'type' => $v->type,
                'create_time' => date("Y-m-d H:i:s",),
                'update_time' => date("Y-m-d H:i:s",)
            ];

            if (count($data) % 5000 == 0) {
                MemberLabel::insert($data);
                $data = [];
            }
        }

        if (count($data) > 0) {
            MemberLabel::insert($data);
        }
    }

    /**
     * 更新产品套餐
     */
    public function updateProductCombo()
    {
        $combos = $this->_connection->table("product_combo")->where("id", ">", 430)->orderBy("id", "asc")->get();
        if (empty($combos))
            return false;

        $comboData = [];
        foreach ($combos as $v) {
            $d = [
                'old_id' => $v->id,
                'title' => $v->title,
                'shop_id' => 10010,
                'product_type' => 1,
//                'amount' => $v->amount,
                'thumb' => $v->thumb,
                'price' => $v->price,
                'summary' => $v->summary,
                'sort' => $v->sort,
                'status' => $v->status,
                'create_time' => $v->create_time,
                'update_time' => $v->update_time,
            ];

            $product_id = Product::create($d)->id;

            if ($product_id) {
                $combo_spec_id = ComboSpec::create([
                    'product_id' => $product_id,
//                    'shop_id' => 10010,
                    'title' => "主菜",
                    'amount' => $v->amount,
                    'create_time' => $v->create_time,
                    'update_time' => $v->update_time,
                ])->id;

                $mapping = $this->_connection->table("product_combo_mapping")->where(['combo_id' => $v->id, 'status' => 1])->get();
                $mappingData = [];
                foreach ($mapping as $sk => $sv) {
                    $mappingData[] = [
                        'spec_id' => $combo_spec_id,
                        'old_id' => $sv->id,
                        'product_id' => $sv->product_id,
                        'sn' => $sv->sn,
                        'amount' => 1,
                        'title' => $sv->title,
                    ];
                }

                ComboSpecItem::insert($mappingData);
            }
        }
    }

    //菜单套餐商品
    public function dailyCombo()
    {
        $menuIds = $this->_connection->table("daily_menu")->where("id", ">", 1489)->where("id", "<", 1600)->orderBy("id", "asc")->pluck("id")->toArray();


        foreach ($menuIds as $k => $menuId) {
            $mappings = $this->_connection->table("menu_product_mapping")
                ->where("menu_id", "=", $menuId)
                ->where(['status' => 1])
                ->orderBy("id", "asc")
                ->get()
                ->toArray();
            if (empty($mappings)) continue;

            $data = [];
            foreach ($mappings as $sk => $sv) {
                if ($sv->type == 1) {
                    $product_id = Product::query()->where("old_id", $sv->product_id)->value("id");
                } else {
                    $product_id = $sv->product_id;
                }

                $data[] = [
                    'old_id' => $sv->id,
                    'menu_id' => $menuId,
                    'category_id' => $sv->category_id,
                    'product_id' => $product_id,
                    'status' => $sv->status,
                    'create_time' => $sv->create_time,
                    'update_time' => $sv->update_time,
                ];
            }

            if (count($data) > 0) {
                MenuProductMapping::insert($data);
            }
        }
    }

    //订单套餐-规格
    public function orderSku()
    {
        if (Cache::get("sku_close")) {
            return true;
        }
        $size = 100;
        // $page = 12;
        $page = Cache::get("sku_page");
        $page = intval($page) + 1;

        $skus = $this->_connection->table("order_product")
            ->where(['type' => 1])
            ->offset($size * ($page - 1))
            ->limit($size)
            // ->where("id", ">", 3288)
            // ->limit(100)
            ->orderBy("id", "asc")
            ->get(['id', 'order_id', 'type', 'product_id', 'product_title'])
            ->toArray();

        if (!isset($skus)) {
            Cache::set("sku_close", 1);
            return true;
        }
        Cache::set("sku_page", $page);

        echo $page;

        $order_skus = [];
        foreach ($skus as $k => $v) {
            $product_id = Product::query()->where("old_id", $v->product_id)->value("id");
            if (empty($product_id)) {
                $product_id = $v->product_id;
            };

            OrderProduct::query()->where("id", $v->id)->update(['product_id' => $product_id]);
            $specs = $this->_connection->table("order_combo_product")->where(['order_product_id' => $v->id])->get()->toArray();
            $combo_id = Product::query()->where(['old_id' => $v->product_id])->value("id");
            $spec_id = ComboSpec::query()->where(['product_id' => $combo_id])->value("id");

            foreach ($specs as $sk => $sv) {
                $spec_item_id = ComboSpecItem::query()->where(['spec_id' => $spec_id, 'product_id' => $product_id])->value("id");

                $order_skus[] = [
                    'order_product_id' => $v->id,
                    'goods_id' => $combo_id,//套餐编号
                    'type' => 1,
                    'spec_id' => $spec_id ?? 0,
                    'spec_item_id' => $spec_item_id ?? 0,
                    'spec_item_title' => $sv->title,
                    'sequence' => $sv->sequence,
                    'amount' => $sv->amount,
                    'order_id' => $v->order_id,
                    'create_time' => $sv->create_time,
//                    'update_time' => $sv->update_time,
                ];

                if (count($order_skus) % 5000 == 0) {
                    OrderSku::insert($order_skus);
                    $order_skus = [];
                }
            }
        }

        if (count($order_skus) > 0) {
            OrderSku::insert($order_skus);
        }

        echo "success";
    }

}